<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Api\BaseController;
use App\Models\Api\User;
use App\Models\Dice;
use App\Models\Divination;
use App\Models\Stage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

class DicesController extends BaseController
{
    /* public function __construct()
    {
    //parent::__construct();// 接口调用验证
    }*/

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 骰子结果
     */
    public function add(Request $request)
    {

        $question = $request->get('content');

        if (empty($question)) {
            return show(config('code.error'), '请描述问题~');
        }
        $xx = Dice::where('type', 1)->get()->toArray();
        shuffle($xx);
        $xx = array_slice($xx, 0, 1);
        $xz = Dice::where('type', 2)->get()->toArray();
        shuffle($xz);
        $xz = array_slice($xz, 0, 1);
        $gw = Dice::where('type', 3)->get()->toArray();
        shuffle($gw);
        $gw    = array_slice($gw, 0, 1);
        $divi  = Divination::create(['type_id' => 2, 'user_id' => $request->uid, 'problem' => $question, 'planet_id' => $xx[0]['id'], 'constellation_id' => $xz[0]['id'], 'palace_id' => $gw[0]['id']]);
        $dices = Divination::with('plant', 'constellation', 'palace')->where('id', $divi->id)->first();
        return show(200, '加载成功', $dices);

    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 骰子详细解释
     */
    public function detail(Request $request, $xx_id, $xz_id, $gw_id)
    {
        $array         = [$xx_id, $xz_id, $gw_id];
        $dices['id']   = $request->input('id');
        $dices['data'] = Dice::whereIn('id', $array)->get();
        return show(200, '加载成功', $dices);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 骰子咨询
     */
    public function consult(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            return show(config('code.error'), 'id不能为空哦~');
        }
        $divination   = Divination::with('plant', 'constellation', 'palace')->where('id', $id)->first();
        $stages = Stage::where('type', 2)->get();
        $user   = User::where(['id' => $request->uid])->first(['id', 'nickname', 'avatar', 'ticket', 'tel']);
        $data   = [
            'divination'=>$divination,
            'stages' => $stages,
            'user'   => $user,
        ];
        return show(200, '加载成功', $data);
    }

}
